//
// Copyright 2023 Google Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//

// stylelint-disable selector-class-pattern --
// Selector '.mdc-*' should only be used in this project.

@use 'sass:math';
@use '@material/dom/dom';
@use '@material/elevation/mixins' as elevation-mixins;
@use '@material/feature-targeting/feature-targeting';
@use '@material/ripple/ripple-theme';
@use '@material/rtl/rtl';
@use '@material/list/mixins' as list-mixins;
@use '@material/list/variables' as list-variables;
@use '@material/ripple/ripple';
@use '@material/theme/theme';
@use '@material/theme/custom-properties';
@use './menu-custom-properties';
@use './variables';
@use '@material/menu-surface/menusurface-theme';

/// @deprecated Use static-styles() instead.
@mixin core-styles($query: feature-targeting.all()) {
  @include static-styles($query);
}

@mixin static-styles($query: feature-targeting.all()) {
  $feat-color: feature-targeting.create-target($query, color);
  $feat-structure: feature-targeting.create-target($query, structure);

  @include ripple.common($query); // COPYBARA_COMMENT_THIS_LINE
  // prettier-ignore
  @include elevation-mixins.overlay-common($query); // COPYBARA_COMMENT_THIS_LINE

  // Customize the menu-surface and contained list to match the mdc-menu styles.
  .mdc-menu {
    @include list-mixins.deprecated-item-meta-ink-color(
      variables.$ink-color,
      $query
    );
    @include list-mixins.deprecated-item-graphic-ink-color(
      variables.$ink-color,
      $query
    );
    @include min-width(variables.$min-width, $query: $query);
    @include open-item-opacity(variables.$open-item-opacity, $query: $query);

    // Include mdc-deprecated-list selector to ensure list-inside-menu overrides default list styles
    .mdc-deprecated-list {
      @include list-mixins.deprecated-item-primary-text-ink-color(
        variables.$ink-color,
        $query
      );
    }

    .mdc-deprecated-list,
    .mdc-list {
      @include elevation-mixins.overlay-surface-position($query: $query);
      @include elevation-mixins.overlay-dimensions(100%, $query: $query);
      @include feature-targeting.targets($feat-structure) {
        border-radius: inherit;
      }
      &::before {
        @include dom.transparent-border($query: $query);
      }
    }

    .mdc-deprecated-list-divider {
      @include feature-targeting.targets($feat-structure) {
        margin: 8px 0;
      }
    }

    .mdc-deprecated-list-item {
      @include feature-targeting.targets($feat-structure) {
        user-select: none;
      }
    }

    .mdc-deprecated-list-item--disabled {
      @include feature-targeting.targets($feat-structure) {
        cursor: auto;
      }
    }

    a.mdc-deprecated-list-item .mdc-deprecated-list-item__text,
    a.mdc-deprecated-list-item .mdc-deprecated-list-item__graphic {
      @include feature-targeting.targets($feat-structure) {
        pointer-events: none;
      }
    }
  }

  // postcss-bem-linter: define menu
  .mdc-menu__selection-group {
    @include feature-targeting.targets($feat-structure) {
      padding: 0;
      fill: currentColor;
    }

    .mdc-deprecated-list-item {
      @include feature-targeting.targets($feat-structure) {
        @include rtl.reflexive-property(
          padding,
          56px,
          list-variables.$deprecated-side-padding
        );
      }
    }

    .mdc-menu__selection-group-icon {
      @include feature-targeting.targets($feat-structure) {
        @include rtl.reflexive-position(left, 16px);

        visibility: hidden;
        position: absolute;
        // IE11 requires the icon to be vertically centered due to its absolute positioning
        top: 50%;
        transform: translateY(-50%);
        // This is an instant transition with a delay that ensures the checkmark
        // appears when menu is closed. Visibility property was used on purpose to
        // indicate that there is no actual transition, but instant shift from start
        // to end state with a delay.
        transition-property: visibility;
        transition-delay: menusurface-theme.$fade-out-duration;
      }
    }
  }
  // postcss-bem-linter: end

  .mdc-menu-item--selected .mdc-menu__selection-group-icon {
    @include feature-targeting.targets($feat-structure) {
      display: inline;
      visibility: visible;
    }
  }
}

@mixin open-item-opacity($opacity, $query: feature-targeting.all()) {
  $feat-color: feature-targeting.create-target($query, color);

  .mdc-menu-item--submenu-open {
    @include ripple-theme.states-background-selector(
      list-variables.$deprecated-ripple-target
    ) {
      @include feature-targeting.targets($feat-color) {
        opacity: $opacity;
      }
    }
    @include ripple-theme.states-background-selector(
      list-variables.$deprecated-temporary-ripple-target
    ) {
      @include feature-targeting.targets($feat-color) {
        opacity: $opacity;
      }
    }
  }
}

@mixin width($width, $query: feature-targeting.all()) {
  $feat-structure: feature-targeting.create-target($query, structure);

  @include feature-targeting.targets($feat-structure) {
    @if math.is-unitless($width) {
      width: $width * variables.$width-base;
    } @else {
      width: $width;
    }
  }
}

/// Sets the min-width of the menu.
/// @param {Number} $min-width - the desired min-width.
@mixin min-width($min-width, $query: feature-targeting.all()) {
  $feat-structure: feature-targeting.create-target($query, structure);

  @include feature-targeting.targets($feat-structure) {
    $min-width-custom-property: custom-properties.create(
      menu-custom-properties.$min-width,
      $min-width
    );
    @include theme.property(min-width, $min-width-custom-property);
  }
}
